import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, LoadingController} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";
import {ValidateProvider} from "../../providers/validate/validate";

@IonicPage({
    segment: 'service/community',
    defaultHistory: ['ServicePage']
})
@Component({
    selector: 'page-community',
    templateUrl: 'community.html',
})
export class CommunityPage {
    nav_id: number = 28;//父级，社区ID 28
    sub_id: any;
    sub_nav = [];
    tab: string;
    banners: any;
    notices: any;
    tabInfo = {
        bbs: {
            cat_id: 20,
            page: 1
        },
        story: {
            cat_id: 21,
            page: 1
        },
        information: {
            cat_id: 23,
            page: 1
        }
    };

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public loadingCtrl: LoadingController,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider,
        public validate: ValidateProvider
    ) {
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewWillEnter(){
        if (this.navParams.get('tab')) {
            this.tab = this.navParams.get('tab');
        } else {
            this.tab = 'service_bbs';
        }
        this.getData();
    }

    getData() {
        this.httpService.requestData('/service/get_ads/type_id/' + this.nav_id, response => {
            //获取Banner和滚动文字信息
            let data = response['data'];
            this.banners = data['image_list'];
            this.notices = data['text_list'];
        });

        this.sub_id = this.navParams.get('id') ? this.navParams.get('id') : this.tabInfo.bbs.cat_id;
        //获取当前分类的默认子分类
        this.httpService.requestData('/service/get_second_nav/nav_key/' + this.nav_id, response => {
            this.sub_nav = response['data'];
            this.getMoreInfo(null);
        });
    }

    changeTab(tab_name, tab_id) {
        this.tab = tab_name;
        this.sub_id = tab_id;
        //切换tab以后重置页码
        this.tabInfo.bbs.page = 1;
        this.tabInfo.story.page = 1;
        this.tabInfo.information.page = 1;
        this.infolist = [];
        this.getMoreInfo(null);
    }

    infolist = [];
    getMoreInfo(infiniteScroll) {
        switch (this.tab) {
            case 'service_bbs':
                this.httpService.requestData('/service/get_info_list/type_id/' + this.sub_id + '/page/' + this.tabInfo.bbs.page, response => {
                    let data = response['data'];
                    this.infolist = this.infolist.concat(data['info_list']['lists']);
                    this.tabInfo.bbs.page++;
                    if (infiniteScroll) {
                        infiniteScroll.complete();
                        /*请求完成数据更新页面*/
                        if (data['info_list']['lists'].length < data['info_list']['page_size']) {   /*没有数据 禁用上拉更新*/
                            infiniteScroll.enable(false);
                        }
                    } else {
                        this.infolist = data['info_list']['lists'];
                    }
                });
                break;
            case 'service_story':
                this.httpService.requestData('/service/get_info_list/type_id/' + this.sub_id + '/page/' + this.tabInfo.story.page, response => {
                    let data = response['data'];
                    this.infolist = this.infolist.concat(data['info_list']['lists']);
                    this.tabInfo.story.page++;
                    if (infiniteScroll) {
                        infiniteScroll.complete();
                        /*请求完成数据更新页面*/
                        if (data['info_list']['lists'].length < data['info_list']['page_size']) {   /*没有数据 禁用上拉更新*/
                            infiniteScroll.enable(false);
                        }
                    } else {
                        this.infolist = data['info_list']['lists'];
                    }
                });
                break;
            case 'service_information':
                this.httpService.requestData('/service/get_info_list/type_id/' + this.sub_id + '/page/' + this.tabInfo.information.page, response => {
                    let data = response['data'];
                    this.infolist = this.infolist.concat(data['info_list']['lists']);
                    this.tabInfo.information.page++;
                    if (infiniteScroll) {
                        infiniteScroll.complete();
                        /*请求完成数据更新页面*/
                        if (data['info_list']['lists'].length < data['info_list']['page_size']) {   /*没有数据 禁用上拉更新*/
                            infiniteScroll.enable(false);
                        }
                    } else {
                        this.infolist = data['info_list']['lists'];
                    }
                });
                break;
            default:
                return;
        }

    }

    doInfinite(infiniteScroll) {
        this.getMoreInfo(infiniteScroll);
    }

}
